"""
@name : ip存活检测
@author : FengZT_19
@projectname: Scxuexi
"""
# 局域网存活IP检测
import subprocess
import IPy
ip = input('请输入你要检查的网段:')
try:
    lst1 = ip.split('.')
    if len(lst1) != 4 or len(lst1[3].split('/')) != 2:
        raise ValueError('输入的格式不正确')
    else:
        ip = IPy.IP(ip)
        with open('ip使用情况.txt', 'a', encoding="utf-8") as fp:
            fp.truncate(0)
            for x in ip:
                p = subprocess.Popen(f'ping -c 1 -w 1 {x}', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                if p.wait():
                    fp.write(f"{x}未使用\n")
                else:
                    fp.write(f"{x}已经被使用\n")
except ValueError as ve:
    print(ve)